Imports Aricie.Documents.Services
Imports Aricie.DNN.Modules.LuceneSearchProvider.IFilter
Imports System.IO
Imports Lucene.Net.Documents
Imports Parsing
Imports DotNetNuke.Common.Utilities


Namespace Aricie.DNN.Modules.LuceneSearch.Business
    ''' <summary>
    ''' Business Controller Class
    ''' </summary>
    Public Class DocumentController

#Region "Regex"

        ''' <summary>
        ''' Regex that match with Html Link
        ''' </summary>
        Private Shared regexLien As Regex = New Regex("<a  .*>(?<Text>.*)</a>", _
                                                       RegexOptions.IgnoreCase _
                                                       Or RegexOptions.CultureInvariant _
                                                       Or RegexOptions.IgnorePatternWhitespace _
                                                       Or RegexOptions.Compiled _
                                                       )

        ''' <summary>
        ''' Regex that replace an html link
        ''' </summary>
        ''' This is the replacement string
        Private Shared regexLienReplace As String = "${Text}"


#End Region


        ''' <summary>
        ''' Read the content of a file using the IFilter process
        ''' </summary>
        ''' <remarks>
        ''' Works for :
        ''' -PDF Files (need Acrobat Reader Installed)
        ''' -Word Files
        ''' -Plain Text Files
        ''' </remarks>
        ''' <param name="currentPDFPath">Path of the file to parse</param>
        ''' <returns>Returns the content of the file as a single string</returns>
        Public Shared Function GetDocumentContentFromIFilter(ByVal currentPDFPath As String) As String
            Dim toReturn As String

            Dim doc As New DocumentFile(currentPDFPath)

            toReturn = doc.ReadDocumentText()

            Return toReturn
        End Function

        ''' <summary>
        ''' Read the content of the CHMFile and return it in a single string
        ''' </summary>
        ''' <param name="chmFilePath">Path of the CHMFile to Parse</param>
        ''' <returns>Contents of the CHMFile used in params</returns>
        Public Shared Function GetDocumentContentFromCHM(ByVal chmFilePath As String) As String
            Dim chmParser As New CHMParser()
            Return chmParser.Parse(chmFilePath)
        End Function


        ''' <summary>
        ''' Get a Lucene.net Document from a file
        ''' </summary>
        ''' <returns>Lucene.net document with the content of the file</returns>
        Public Shared Function GetDocumentFromFile(ByVal documentPath As String, ByVal store As Boolean) As Document


            Dim toReturn As New Document()

            For Each objField As Field In GetFieldsFromFile(documentPath, store)
                toReturn.Add(objField)
            Next

            Return toReturn
        End Function

        ''' <summary>
        ''' Get a Lucene.net Document from a file
        ''' </summary>
        ''' <returns>Lucene.net document with the content of the file</returns>
        Public Shared Function GetDocumentFromStream(ByVal fileExtension As String, ByVal documentStream As Stream, ByVal store As Boolean) As Document


            Dim toReturn As New Document()
            Dim objFile As New DocumentFile(fileExtension, documentStream)
            For Each objField As Field In GetFieldsFromFile(objFile.FileName, store)
                toReturn.Add(objField)
            Next

            Return toReturn
        End Function


        Public Shared Function GetFieldsFromFile(ByVal documentPath As String, ByVal store As Boolean) As List(Of Field)

            Dim toReturn As New List(Of Field)

            Dim content As String = ""

            If documentPath.EndsWith("chm") Then
                content = GetDocumentContentFromCHM(documentPath)

            Else
                content = GetDocumentContentFromIFilter(documentPath)
            End If


            If content <> String.Empty Then
                'suppression de liens internes au document
                content = regexLien.Replace(content, regexLienReplace)

                content = HtmlUtils.StripTags(content, True)
                Dim fileName As String = System.IO.Path.GetFileNameWithoutExtension(documentPath)


                Dim factory As FieldFactory = FieldFactory.Instance

                toReturn.Add(factory.CreateField(LuceneGeneralField.FileName, fileName))
                If store Then
                    toReturn.Add(factory.CreateField(LuceneGeneralField.Text, content))
                Else
                    toReturn.Add(factory.CreateField(LuceneGeneralField.TextNoStore, content))
                End If


            End If

            Return toReturn
        End Function


    End Class
End Namespace


